package com.chenfan.magic.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.chenfan.magic.utils.AES;
import com.chenfan.magic.utils.StringKit;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;
import java.util.Date;

/**
 * @author troy
 */
@Data
@TableName("idc_datasource")
@ApiModel(value = "Datasource对象", description = "Datasource对象")
public class IdcDatasource implements Serializable {

    private static final long serialVersionUID = 1L;

    private static final String ENCRYPT_KEY = "#62a51ed179969dd3af28434b2e967913";

    @TableId(value = "id", type = IdType.ID_WORKER_STR)
    @ApiModelProperty(value = "id")
    private String id;

    @ApiModelProperty(value = "创建者")
    private String createBy;

    @ApiModelProperty(value = "创建时间")
    private Date createTime;

    @ApiModelProperty(value = "更新者")
    private String updateBy;

    @ApiModelProperty(value = "更新时间")
    private Date updateTime;

    @ApiModelProperty(value = "状态")
    private Integer status;

    @ApiModelProperty(value = "逻辑标记")
    private Integer logicDel;

    @ApiModelProperty(value = "数据源类型 id")
    private String datasourceTypeId;

    @ApiModelProperty(value = "作者用户 id (所属用户 id)")
    private Long authorId;

    @ApiModelProperty(value = "数据源名称")
    private String name;

    @ApiModelProperty(value = "数据源描述")
    private String description;

    @ApiModelProperty(value = "数据源连接串")
    private String url;

    @ApiModelProperty(value = "用户名")
    private String username;

    @ApiModelProperty(value = "密码")
    private String password;

    @ApiModelProperty(value = "公匙")
    private String publicKey;

    @ApiModelProperty(value = "私匙")
    private String privateKey;

    @ApiModelProperty(value = "*")
    private int initialSize;

    @ApiModelProperty(value = "*")
    private int minIdle;

    @ApiModelProperty(value = "*")
    private int maxActive;

    @ApiModelProperty(value = "*")
    private int maxWait;

    @ApiModelProperty(value = "*")
    private int timeBetweenEvictionRunsMillis;

    @ApiModelProperty(value = "*")
    private int minEvictableIdleTimeMillis;

    @ApiModelProperty(value = "*")
    private String validationQuery;

    @ApiModelProperty(value = "*")
    private String testWhileIdle;

    @ApiModelProperty(value = "*")
    private String testOnBorrow;

    @ApiModelProperty(value = "*")
    private String testOnReturn;

    @ApiModelProperty(value = "*")
    private String poolPreparedStatements;

    @ApiModelProperty(value = "*")
    private int maxPoolPreparedStatementPerConnectionSize;

    @ApiModelProperty(value = "*")
    private String filters;

    @ApiModelProperty(value = "*")
    private String connectionProperties;

    @ApiModelProperty(value = "*")
    private String xdual;

    @ApiModelProperty(value = "元数据锁")
    private int metaLock;

    @ApiModelProperty(value = "保活开关")
    private String keepAlive;

    @ApiModelProperty(value = "连接出错后再尝试连接次数")
    private int connectionErrorRetryAttempts;

    @ApiModelProperty(value = "数据库服务宕机自动重连机制")
    private String breakAfterAcquireFailure;

    @ApiModelProperty(value = "异步初始化策略")
    private String asyncInit;

    @ApiModelProperty(value = "连接出错后重试时间间隔")
    private int timeBetweenConnectErrorMillis;

    @ApiModelProperty(value = "每隔多久收集一次监控日志")
    private int timeBetweenLogStatsMillis;

    @ApiModelProperty(value = "累计使用次数")
    private Long applyTimes;

    @ApiModelProperty(value = "最近一次使用时间")
    private Date applyNearlyTime;

    @ApiModelProperty(value = "是否收集监控信息")
    private int gatherMonitor;

    @ApiModelProperty(value = "是否收集统计信息")
    private int gatherCensus;

    public String getUrlDecrypt(String sercet) {
        if (StringKit.isNotBlank(url) && url.trim().startsWith(ENCRYPT_KEY)) {
            url = AES.decrypt(url.replace(ENCRYPT_KEY, ""), sercet);
        }
        return url;
    }

    public void setUrlEncrypt(String url, String sercet) {
        if (StringKit.isNotBlank(url) && !url.trim().startsWith(ENCRYPT_KEY)) {
            url = ENCRYPT_KEY + AES.encrypt(url, sercet);
        }
        this.url = url;
    }

    public String getPasswordDecrypt(String sercet) {
        if (StringKit.isNotBlank(password) && password.trim().startsWith(ENCRYPT_KEY)) {
            password = AES.decrypt(password.replace(ENCRYPT_KEY, ""), sercet);
        }
        return password;
    }

    public void setPasswordEncrypt(String password, String sercet) {
        if (StringKit.isNotBlank(password) && !password.trim().startsWith(ENCRYPT_KEY)) {
            password = ENCRYPT_KEY + AES.encrypt(password, sercet);
        }
        this.url = password;
    }

    /**
     * 物理连表Query生成
     *
     * @return
     */
//    public static Query getJoinQuery() {
//        return new Query()
//                // 左连接查询,主表的alias默认为t
//                .join("LEFT JOIN idc_user t2 ON t.author_id = t2.idc_user_id")
//                .join("RIGHT JOIN idc_datasource_type t3 ON t.datasource_type_id = t3.id");
//    }
//    public static List<String> getColumns() {
//        return Arrays.asList("t.*", "t2.*", "t3.*");
//    }
}
